/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.txt.access.capability.spi.resource.amqp.model;

import it.txt.access.capability.spi.resource.exception.URISyntaxException;
import it.txt.access.capability.spi.resource.model.ICapabilityResourceIDModel;

import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.client.utils.URIBuilder;

/**
 *
 * @author seccia
 */
public class AMQPResourceIDModel implements ICapabilityResourceIDModel{

    private String scheme;
    private String host;
    private int port = UNDEFINED_PORT;
    private String service;
    private String namespace;
    private String pattern;
    private static final String COLON = ":";
    private static final int UNDEFINED_PORT = -1;
    private static final String NAMESPACE_QUERY_PARAM = "Namespace";
    private static final String PATTERN_QUERY_PARAM = "Pattern";

    public String getAuthority() {
        if (port > UNDEFINED_PORT)
            return host + COLON + port;
        else
            return host;
    }

    public void setAuthority(String authority) {
        int colonIndex = authority.indexOf(COLON);
        if (colonIndex == -1)
            this.host = authority;
        else {
            this.host = authority.substring(0, colonIndex);
            this.port = Integer.parseInt(authority.substring(colonIndex + COLON.length()));
        }
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    @Override
    public String toString(){
        try {
            return  toURI().toString();
        } catch (URISyntaxException e) {
            Logger.getLogger(AMQPResourceIDModel.class.getName()).log(Level.SEVERE, e.getMessage(), e);
            return "<BAD URI>";
        }
    }

    public URI toURI() throws URISyntaxException{
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.addParameter(NAMESPACE_QUERY_PARAM, this.namespace);
        uriBuilder.addParameter(PATTERN_QUERY_PARAM, this.pattern);
        uriBuilder.setHost(this.host);
        if (port > UNDEFINED_PORT)
            uriBuilder.setPort(port);
        uriBuilder.setScheme(this.scheme);
        uriBuilder.setPath((!this.service.startsWith("/") ? "/" : "") + this.service);
        try {
            return uriBuilder.build();
        } catch (java.net.URISyntaxException e) {
            throw new URISyntaxException(e);
        }
    }
}
